package com.sjsoft.video;


import java.util.ArrayList;

import com.sjsoft.Utils.networkUtility;
import com.sjsoft.application.chatVideoApp;
import com.sjsoft.chat.video.mainTab;
import com.sjsoft.model.CSubCategory;
import com.sjsoft.webservice.UtilsJson;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class LoadVideoInfo extends AsyncTask<Void, Void, Void>{

	
	protected chatVideoApp application;
	int resultRequest;	
	boolean isNetWork = true;
	String sExtendId = "";
	Context mContext;
	//Constructor
	
	private ProgressDialog pDlg = null;	
	
	public LoadVideoInfo(Context context) {
		mContext = context;
		isNetWork = networkUtility.getInstance(context)
				.isNetworkAvailable();
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();	
		showProgressDialog(); 
	}

	
	private void showProgressDialog() {
		if(mContext != null)
		{
			pDlg = new ProgressDialog(mContext);
			pDlg.setMessage("Loading data");
			pDlg.setProgressDrawable(mContext.getWallpaper());
			pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pDlg.setCancelable(false);
			pDlg.show();
		}
		
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		if (isNetWork)
		{
			//application.videoCategory = UtilsJson.getCategoryVideo();
			String sCategory = UtilsJson.getExtendId();
			if(sCategory != null)
			{
				ArrayList<CSubCategory> list =  UtilsJson.getListSubByExtendId(sCategory);			
				if(list.size() > 0)
					sExtendId = list.get(0).getExtendId();
			}
			else
			{
				chatVideoApp.appendLog("get root video id  is null ");
			}
		} 
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		chatVideoApp.appendLog("get category video = ");
		LoadVideoCategory loadVideoCategory = new LoadVideoCategory(mContext,sExtendId);
		loadVideoCategory.execute();
	}
	
	
	public class LoadVideoCategory extends AsyncTask<Void, Void, Void>{

		
		protected chatVideoApp application;
		int resultRequest;	
		boolean isNetWork = true;
		String id;
		Context mContext;
		//Constructor
		
		public LoadVideoCategory(Context context,String sId) {
			this.id = sId;
			mContext = context;
			isNetWork = networkUtility.getInstance(context)
					.isNetworkAvailable();
			this.application = (chatVideoApp) context.getApplicationContext();
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();		
		}

		@Override
		protected Void doInBackground(Void... params) {
			if (isNetWork) {
				//application.videoCategory = UtilsJson.getCategoryVideo();
				String sCategory = UtilsJson.getCategoryVideo(id);
				if(sCategory != null)
				{
					application.videoByCategory = UtilsJson.getListSubByExtendId(sCategory);
					
					chatVideoApp.appendLog("Total tab = "+application.videoByCategory.size());
				}
			} 
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			((mainTab)mContext).updateTab();
			///CSubCategory firstItem = application.videoByCategory.get(0);
			//LoadItemsVideo loadVideo = new LoadItemsVideo(mContext, firstItem.getExtendId(), 0,pDlg);
			//loadVideo.execute();	
			pDlg.dismiss();
		}
	}
	
	
}
